// 子模块 聊天数据
import {
	VUE_APP_WS_URL
} from '@/config/app.js';


export default {
	state: {
		SocketTask: null,
		AUDIO: uni.createInnerAudioContext(),
		ErrorMsg: [],
		MaxRetryCount: 300, // 最大重连次数
		CurrentRetryCount: 0,
		url: VUE_APP_WS_URL,
		ortherdata: null, //其他通用数据，含个人信息
		tempdata: null,

		//聊天列表数据
		session_data: {
			session_list: {
				total: 0,
				current_page: 0,
				last_page: 1,
				data: []
			}, //聊天列表数据
			session_time: 0, //当前最小时间
			session_id: 0, //当前最小id
		},
		//黑名单列表
		blacklist: {
			total: 0,
			current_page: 0,
			last_page: 1,
			data: []
		},
		// 通知列表
		noticelist: {
			total: 0,
			current_page: 0,
			last_page: 1,
			data: []
		},
		// 聊天对话数据
		msg_list: {
			receive_user: null, //接收人
			receive_user_id: 0, //接收人ID
			all_img_arr: [], //所有可以放大的图片
			loadingList: {
				total: 0,
				current_page: 0,
				last_page: 1,
				data: []
			},
		},

		session_list_callback: null, //获取列表回调数据
		search_session_list_callback: null, //获取列表回调数据
		new_session_list_callback: null, //新增列表回调数据
		msg_list_callback: null, //聊天记录回调数据
		new_msg_callback: null,
		changenotice_callback: null,
	},
	getters: {

	},
	mutations: {
		// 退出登录初始化列表
		INITIALIZATION_LIST: (state) => {
			state.session_data = {
				session_list: {
					total: 0,
					current_page: 0,
					last_page: 1,
					data: []
				}, //聊天列表数据
				session_time: 0, //当前最小时间
				session_id: 0, //当前最小id
			};
			state.blacklist = {
				total: 0,
				current_page: 0,
				last_page: 1,
				data: []
			};
			state.noticelist = {
				total: 0,
				current_page: 0,
				last_page: 1,
				data: []
			};
			state.msg_list = {
				receive_user: null, //接收人
				receive_user_id: 0, //接收人ID
				all_img_arr: [], //所有可以放大的图片
				loadingList: {
					total: 0,
					current_page: 0,
					last_page: 1,
					data: []
				},
			}
		},
		// 设置当前聊天列表数据
		SET_SESSION_LIST: (state, session_data) => {
			state.session_data = session_data;
		},
		// 设置已读
		SET_READ: (state, data) => {
			var read_num = 0;
			// 处理会话列表数据
			var now_data = state.session_data.session_list.data;
			for (let index = 0; index < now_data.length; index++) {
				const element = now_data[index];
				if (element.id == data.session_id) {
					read_num = now_data[index].unread_num;
					now_data[index].unread_num = 0;
				}
			}
			// console.log(state.session_list,"设置已读");
			// return
			state.session_data.session_list.data = now_data;
			state.ortherdata.unread_num = state.ortherdata
				.unread_num - read_num;
		},
		// 设置新的聊天消息
		SET_NEW_MSG: (state, data) => {
			if (data.receive_user_id != state.msg_list.receive_user_id &&
				data.send_user_id != state.msg_list.receive_user_id) {
				return false;
			}
			var loadingList = state.msg_list.loadingList;
			var now_loadingList_data = loadingList.data || [];
			var ret = handleMsgData(data, state.ortherdata.chat_memes);
			var now_loadingList_data = now_loadingList_data.concat(ret.data);
			loadingList.data = now_loadingList_data;
			var all_img_arr = state.msg_list.all_img_arr || [];
			all_img_arr = all_img_arr.concat(ret.images_arr);

			state.msg_list.loadingList = loadingList
			state.msg_list.all_img_arr = all_img_arr

			// 如果在聊天对话页面，接收到数据，设置一下已读。
			// 下面代码复制到页面里使用，这边不能使用
			// 页面滚动到底部---这里需要写一个方法
			// if (data.send_user_id == state.msg_list.receive_user_id) {
			// store.dispatch('ws_send',{
			// 	type: 'setread',
			// 	session_id: state.msg_list.session_id,
			// 	receive_user_id: state.msg_list.receive_user_id
			// });
			// }
		},
		// 设置当前聊天记录
		SET_MSG_LIST: (state, res) => {
			if (state.msg_list.receive_user_id != res.receive_user.id) {
				state.msg_list = {
					receive_user: null, //接收人
					receive_user_id: 0, //接收人ID
					all_img_arr: [], //所有可以放大的图片
					loadingList: {
						total: 0,
						current_page: 0,
						last_page: 1,
						data: []
					},
				};
			}
			let loadingList = res.data;
			console.log(state.msg_list.loadingList.last_page, "last_page");
			// if (state.msg_list.loadingList.current_page > 1) {
			var now_loadingList_data = state.msg_list.loadingList.data || [];
			// console.log(now_loadingList_data,"now_loadingList_data");
			// console.log(loadingList,"loadingList");
			// now_loadingList_data = now_loadingList_data.concat(loadingList.data);
			now_loadingList_data = [...loadingList.data, ...now_loadingList_data]
			loadingList.data = now_loadingList_data;
			// }
			var all_img_arr = state.msg_list.all_img_arr || [];
			all_img_arr = all_img_arr.concat(res.images_arr);
			state.msg_list = {
				loadingList: loadingList,
				all_img_arr: all_img_arr,
				receive_user: res.receive_user,
				receive_user_id: res.receive_user.id,
			};
			console.log(state.msg_list, "最后9999999999999999999999999")
			// 如果在聊天对话页面，接收到数据，设置一下已读。
			// 下面代码复制到页面里使用，这边不能使用
			// store.dispatch('ws_send',{
			// 	type: 'setread',
			// 	session_id: state.msg_list.session_id,
			// 	receive_user_id: state.msg_list.receive_user_id
			// });
		},
		SET_SOCKETTASK: (state, socketTask) => {
			state.SocketTask = socketTask
		},
		SET_WSS_URL: (state, token) => {
			state.url = VUE_APP_WS_URL + '?token=' + token
		},
		SET_CURRENTRETRYCOUNT: (state, CurrentRetryCount) => {
			state.CurrentRetryCount = CurrentRetryCount
		},
		ADD_CURRENTRETRYCOUNT: (state) => {
			state.CurrentRetryCount++
		},
		SET_ERRORMSG: (state, ErrorMsg) => {
			state.ErrorMsg = ErrorMsg
		},
		PUSH_ERRORMSG: (state, ErrorMsg) => {
			state.ErrorMsg.push(ErrorMsg)
		},
		INIT_SESSIONLIST: (state) => {
			state.session_data.session_time = 0;
			state.session_data.session_id = 0;
			state.session_data.session_list = {
				total: 0,
				current_page: 0,
				last_page: 1,
				data: []
			};
		},
		SET_Ortherdata: (state, data) => {
			state.ortherdata = Object.assign({}, state.ortherdata, data);
			// console.log(state.ortherdata,"惊雷");
		},
		// 设置黑名单列表
		SET_BLACKLIST: (state, loadingList) => {
			var now_loadingList_data = state.blacklist.data || [];
			var now_loadingList_data = now_loadingList_data.concat(loadingList.data);
			loadingList.data = now_loadingList_data;
			state.blacklist = loadingList
		},
		// 移除黑名单
		DEL_BLACKLIST: (state, res) => {
			var loadingList = state.blacklist.loadingList;
			for (let index = 0; index < loadingList.data.length; index++) {
				const element = loadingList.data[index];
				if (res.blacklist_id == element.id) {
					loadingList.data.splice(index, 1);
				}
			}
			// showToast('移除成功');
			state.blacklist = loadingList
		},
		// 设置通知列表
		SET_NOTICELIST: (state, loadingList) => {
			var now_loadingList_data = state.noticelist.data || [];
			if(loadingList.current_page==1){
				var now_loadingList_data = loadingList.data;
			}else{
				var now_loadingList_data = now_loadingList_data.concat(loadingList.data);
			}
			// var now_loadingList_data = loadingList.data;
			loadingList.data = now_loadingList_data;
			state.noticelist = loadingList;
		},
		// 隔一段时间修改聊天列表的时间字段
		changeTimeText: (state) => {
			var now_data = state.session_data.session_list.data;
			for (let index = 0; index < now_data.length; index++) {
				now_data[index].updatetime_text = formatMsgTime(now_data[index].updatetime * 1000);
			}
			state.session_data.session_list.data = now_data;
		},
		onSocketClose(state) {
			console.log("进来了");
			state.SocketTask.close()
			uni.onSocketClose(function(res) {
				console.log('WebSocket 已关闭！', res);
			});
		},
	},
	actions: {
		// 立即链接 Websocket
		connectSocket: function({
			commit,
			dispatch,
			state
		}, token = '') {
			console.log(token, "websocket开始连接")
			let appGlobalData = getApp().globalData
			if (token) {
				commit('SET_WSS_URL', token);
			}
			if (state.SocketTask && state.SocketTask.readyState == 1) {
				return false;
			}
			console.log("没有return了   未执行")
			let socketTask = uni.connectSocket({
				url: state.url,
				success: () => {
					console.log('wss 连接成功')
					commit('INITIALIZATION_LIST')
				}
			});
			commit('SET_SOCKETTASK', socketTask)

			socketTask.onMessage((evt) => {
				let evtdata = JSON.parse(evt.data);
				// if (evtdata.data.data) {
				// 	evtdata.data.data.forEach(item => {
				// 		if (item.chat_type == 4) {
				// 			state.AUDIO.src = item.content_text
				// 			setTimeout(()=>{
				// 				item["duration"] = Math.floor(state.AUDIO.duration)
				// 			},500)
				// 		}
				// 	})
				// }
				// console.log(evtdata.data.data, 88888888888888888888);
				console.log(evtdata, 99999999999999);
				dispatch('handleMsg', JSON.parse(evt.data))
				// evtdata = '';
			})
			socketTask.onOpen(() => {
				commit('SET_CURRENTRETRYCOUNT', 0)
				// 重新发送所有出错的消息
				if (state.ErrorMsg.length > 0) {
					for (let i in state.ErrorMsg) {
						dispatch('ws_send', state.ErrorMsg[i])
					}
					commit('SET_ERRORMSG', [])
				}
				if (appGlobalData.Timer != null) {
					clearInterval(appGlobalData.Timer);
				}
				//定时发送心跳
				appGlobalData.Timer = setInterval(() => {
					dispatch('ws_send', '')
				}, 50000);
			})
			socketTask.onError(function(res) {
				console.log('wss:onError', res);
			})
			socketTask.onClose(function(res) {
				console.log('关闭', res);
				if (appGlobalData.Timer != null) {
					// commit('CLEAR_Interval')
					clearInterval(appGlobalData.Timer);
				}
				if (state.MaxRetryCount) {
					// commit('SET_Interval', dispatch('retryWebSocket'), 3000)
					appGlobalData.Timer = setInterval(() => {
						dispatch('retryWebSocket')
					}, 3000);
				}
			})
		},
		// 重连wss
		retryWebSocket: function({
			commit,
			dispatch,
			state
		}) {
			let appGlobalData = getApp().globalData
			if (state.CurrentRetryCount < state.MaxRetryCount) {
				commit('ADD_CURRENTRETRYCOUNT')
				dispatch('connectSocket')
				console.log('重连 WebSocket 第' + state.CurrentRetryCount + '次');
			} else {
				if (appGlobalData.Timer != null) {
					// commit('CLEAR_Interval')
					clearInterval(appGlobalData.Timer);
				}
				if (state.ReConnection) {
					console.log('每隔10秒将再次尝试重连 WebSocket')
					//每10秒重新连接一次
					// commit('SET_Interval', dispatch('connectSocket'), 10000)
					appGlobalData.Timer = setInterval(() => {
						dispatch('connectSocket')
					}, 10000);
				}
			}
		},
		//发送消息
		ws_send: function({
			commit,
			dispatch,
			state
		}, message) {
			if (!message) {
				message = {
					type: 'ping'
				};
				commit('changeTimeText')
			}
			console.log('send:', message);
			if (state.SocketTask && state.SocketTask.readyState == 1) {
				state.SocketTask.send({
					data: JSON.stringify(message),
					success: res => {
						// console.log(res, "发送成功12");
					},
					fail: res => {
						console.error(res, "发送失败");
						// 提示当前消息报错
						commit('PUSH_ERRORMSG', message)
					}
				});
			} else {
				console.log('消息发送出错', message)
				// 提示当前消息报错
				commit('PUSH_ERRORMSG', message)
				// 重连websocket
				dispatch('retryWebSocket')
			}
		},
		sendMessage: function({
			dispatch
		}, {
			receive_user_id,
			session_id,
			content,
			chat_type = 1
		}) {
			dispatch('ws_send', {
				type: 'say',
				receive_user_id: receive_user_id,
				session_id: session_id,
				content: content,
				chat_type: chat_type, //类型:1=文字,2=图片,3=视频,4=音频,5=语音通话,6=视频通话
			});
		},
		// 获取聊天对话页面数据
		getMsgList: function({
			dispatch
		}, {
			session_id,
			receive_user_id,
			page
		}) {
			dispatch("ws_send", {
				type: 'msglist',
				page: page,
				session_id: session_id, //会话列表id
				receive_user_id: receive_user_id, //接收人
			})
		},
		// 获取会话列表页面数据
		getSessionList: function({
			dispatch,
			state
		}) {
			dispatch('ws_send', {
				type: 'sessionlist',
				session_time: state.session_data.session_time,
				session_id: state.session_data.session_id,
			});
		},
		// 获取系统通知
		getNoticeList: function({
			dispatch,
			state
		}, {
			page
		}) {
			dispatch('ws_send', {
				type: 'noticelist',
				page: page,
				nowtype: 'system'
			})
		},
		// 撤回消息
		backMessage: function({
			commit,
			dispatch,
			state
		}, {
			session_id,
			receive_user_id,
			msg_id,
			page
		}) {
			dispatch('ws_send', {
				type: 'backmsg',
				session_id,
				receive_user_id,
				msg_id
			})
		},
		// getSessionList:function({
		// 	dispatch,
		// 	state
		// }){
		// 	dispatch('ws_send',{
		// 		type:"sessionlist",
		// 		session_time:state.session_data.session_time,
		// 		session_id:state.session_data.session_id
		// 	})
		// },
		// 处理接收消息
		handleMsg: function({
			commit,
			dispatch,
			state
		}, ret) {
			console.log(ret, "惊雷");
			switch (ret.type) {
				case 'login':
					console.log('登录成功了！');
					commit('SET_Ortherdata', ret.data);
					// 获取第一页会话列表
					commit('INIT_SESSIONLIST');
					dispatch('ws_send', {
						type: 'sessionlist',
						session_time: 0,
						session_id: 0,
					})
					break;
				case 'session_list':
					// console.log(ret,"聊天列表");
					if (ret.code == 0) {
						showToast(ret.msg);
						return;
					}
					// 处理会话列表
					var data = handleSessionList(ret.data, state.session_data)
					commit('SET_SESSION_LIST', data);
					break;
				case 'msg_list':
					// 处理聊天记录列表
					var data = handleMsgList(ret, state.ortherdata.chat_memes)
					// console.log(data,"聊天记录")
					commit('SET_MSG_LIST', data);
					break;
				case 'del_session':
					if (ret.code != 1) {
						showToast(ret.msg);
						return false;
					}
					// 删除会话
					var data = delSessionList(ret.data, state.session_data)
					commit('SET_SESSION_LIST', data);
					break;
				case 'join_blacklist':
					if (ret.code != 1) {
						showToast(ret.msg);
						return false;
					}
					// 拉黑用户会话
					// 删除会话
					var data = delSessionList(ret.data, state.session_data)
					commit('SET_SESSION_LIST', data);
					break;
				case 'blacklist':
					// 黑名单列表
					commit('SET_BLACKLIST', ret.data);
					break;
				case 'del_blacklist':
					if (ret.code != 1) {
						that.info(ret.msg);
						return false;
					}
					// 移除黑名单
					commit('DEL_BLACKLIST', ret.data);
					// 获取第一页会话列表
					commit('INIT_SESSIONLIST');
					dispatch('ws_send', {
						type: 'sessionlist',
						session_time: 0,
						session_id: 0,
					})
					break;
				case 'notice_list':
					// 通知列表
					commit('SET_NOTICELIST', ret.data);
					// console.log(res.data, "notice_list")
					break;
				case 'new_initdata':
					// 新初始化数据
					commit('SET_Ortherdata', ret.data);
					break;
				case 'new_notice':
					// 新通知消息
					var ortherdata = state.ortherdata;
					ortherdata.message_num++;
					ortherdata.unread_num++;
					commit('SET_Ortherdata', ortherdata);
					break;
				case 'new_msg':
					// 处理会话列表数据
					var data = newSessionList(ret.data, state.session_data, state.ortherdata)
					commit('SET_SESSION_LIST', data[0]);
					commit('SET_Ortherdata', data[1]);
					// 处理消息对话页面数据
					commit('SET_NEW_MSG', ret.data);
					// let obj=plus.device.getVolume()
					console.log(plus.device.getVolume(),"手机音量")
					let main = plus.android.runtimeMainActivity();
					let RingtoneManager = plus.android.importClass("android.media.RingtoneManager");
					let uri = RingtoneManager.getActualDefaultRingtoneUri(main, RingtoneManager
						.TYPE_NOTIFICATION);
					let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
					let player = MediaPlayer.create(main, uri);
					player.setLooping(false);
					player.prepare();
					player.start();
					uni.vibrateLong({
						success: function() {
							console.log('success');
						}
					});
					break;
				case 'back_msg':
					console.log(ret, "back_msg");
					if (ret.code == 0) {
						showToast(ret.msg);
						return;
					} else if (ret.code == 1) {
						showToast("撤回成功");
						// dispatch("getMsgList", {
						// 	session_id: ret.data.session_id,
						// 	receive_user_id: ret.data.receive_user_id,
						// 	page: state.page
						// })
						state.msg_list.loadingList.data.forEach((item, index) => {
							item.data.forEach((ite, i) => {
								if (ite.id == ret.data.msg_id) {
									if (item.data.length == 1) {
										state.msg_list.loadingList.data.splice(index, 1)
									} else {
										item.data.splice(i, 1)
									}
								}
							})
						})
						// console.log(index,"index");
					}
					break;
				case 'new_back_msg':
					if (ret.code == 0) {
						showToast(ret.msg);
						return;
					} else if (ret.code == 1) {
						showToast("撤回成功");
						state.msg_list.loadingList.data.forEach((item, index) => {
							item.data.forEach((ite, i) => {
								if (ite.id == ret.data.msg_id) {
									if (item.data.length == 1) {
										state.msg_list.loadingList.data.splice(index, 1)
									} else {
										item.data.splice(i, 1)
									}
								}
							})
						})
						let main = plus.android.runtimeMainActivity();
						let RingtoneManager = plus.android.importClass("android.media.RingtoneManager");
						let uri = RingtoneManager.getActualDefaultRingtoneUri(main, RingtoneManager
							.TYPE_NOTIFICATION);
						let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
						let player = MediaPlayer.create(main, uri);
						player.setLooping(false);
						player.prepare();
						player.start();
						uni.vibrateLong({
							success: function() {
								console.log('success');
							}
						});
						console.log(ret, "back_msg");
					}
					break;
				case 'say':
					console.log(ret);
					if (ret.code == 0) {
						showToast(ret.msg);
						return;
					}
					// 处理会话列表数据
					var data = newSessionList(ret.data, state.session_data, state.ortherdata)
					console.log(data);
					commit('SET_SESSION_LIST', data[0]);
					commit('SET_Ortherdata', data[1]);
					commit('SET_NEW_MSG', ret.data);
					break;
				case 'set_read':
					commit('SET_READ', ret.data);
					break;
				default:
					break;
			}
		},

	}
}
// 处理新的聊天消息
function newSessionList(data, session_data, ortherdata) {

	var now_data = session_data.session_list.data;
	var user_id = ortherdata.user_info.user_id;
	var unread_num = ortherdata.unread_num;
	var is_exist = false;
	// 判断是否存在该会话
	for (let index = 0; index < now_data.length; index++) {
		const element = now_data[index];
		if (element.id == data.session_id) {
			is_exist = true;
			var session_info = now_data[index];
			session_info.newmsg.content_text2 = data.content_text2;
			session_info.updatetime_text = data.createtime_text;
			session_info.updatetime = data.createtime;
			// 判断未读个数
			if (user_id == data.receive_user_id) {
				unread_num++;
				session_info.unread_num++;
			}
			now_data.splice(index, 1);
			now_data.unshift(session_info);

		}
	}
	// 创建新的会话数据
	if (!is_exist) {
		var session_info = {
			id: data.session_id,
			receive_user_id: data.receive_user_id,
			send_user_id: data.send_user_id,
			receiveuser: data.receiveuser,
			senduser: data.senduser,
			updatetime_text: data.createtime_text,
			updatetime: data.createtime,
			unread_num: (user_id == data.receive_user_id) ? 1 : 0,
			see_type: 0,
			newmsg: {
				content_text2: data.content_text2
			},
		};
		now_data.unshift(session_info);
		if (user_id == data.receive_user_id) {
			unread_num++;
		}
	}
	session_data.session_list.data = now_data;
	ortherdata.unread_num = unread_num;
	return [session_data, ortherdata];
}
// 处理聊天列表
function handleSessionList(data, session_data) {
	if (session_data.session_time == 0 && session_data.session_id == 0 && (!session_data.session_list || session_data
			.session_list.total == 0)) {
		session_data.session_list = data;
	} else {
		var now_data = session_data.session_list.data;
		now_data = now_data.concat(data.data);
		data.data = now_data;
		session_data.session_list = data;
	}
	// 获取最小时间和id
	var session_list_data = session_data.session_list.data;
	var len = session_list_data.length;
	session_data.session_time = len > 0 ? session_list_data[len - 1].updatetime : 0;
	session_data.session_id = len > 0 ? session_list_data[len - 1].id : 0;
	return session_data;
}
// 删除聊天列表数据
function delSessionList(data, session_data) {
	var now_data = session_data.session_list.data;
	for (let index = 0; index < now_data.length; index++) {
		const element = now_data[index];
		if (data.session_id == element.id) {
			now_data.splice(index, 1);
		}
	}
	session_data.session_list.data = now_data;
	return session_data;
}

// 处理聊天对话页面数据
function handleMsgList(ret, emotions) {
	// console.log(ret,"7777");
	var res = ret.data;
	if (res.total == 0) {
		return {
			'data': res,
			'images_arr': [],
			'receive_user': ret.receiveuser
		};
	}
	var array = [];
	var new_arr = [];
	var min_time = res.data[res.data.length - 1].createtime;
	var min_time_text = res.data[res.data.length - 1].createtime_text;
	array[min_time_text] = [];
	var images_arr = [];
	for (let index = res.data.length - 1; index >= 0; index--) {
		const v = res.data[index];
		if (v.chat_type == 2) {
			images_arr.push(v.content_text);
		}
		// 处理表情包
		v.content_text = v.chat_type == 1 ? changeEmotion(v.content_text, emotions) : [v.content_text];
		// 如果当前时间和最早时间相差不超过10分钟，就分为一组
		if ((v.createtime - min_time) <= 600) {
			array[min_time_text].push(v)
		} else {
			min_time = v.createtime;
			min_time_text = v.createtime_text;
			array[min_time_text] = [];
			array[min_time_text].push(v)
		}
	}
	for (const key in array) {
		new_arr.push({
			time: key,
			data: array[key]
		});
	}
	res.data = new_arr;
	return {
		'data': res,
		'images_arr': images_arr,
		'receive_user': ret.receiveuser
	};
}

function handleMsgData(data, emotions) {
	var new_arr = [];
	var images_arr = [];
	if (data.chat_type == 2) {
		images_arr.push(data.content_text);
	}
	// 处理表情包
	data.content_text = data.chat_type == 1 ? changeEmotion(data.content_text, emotions) : [data.content_text];
	new_arr.push({
		time: data.createtime_text,
		data: [data]
	});
	return {
		'data': new_arr,
		'images_arr': images_arr
	};
}
// 处理表情包显示
function changeEmotion(contents, emotions) {
	var pattern1 = /\[[\u4e00-\u9fa5]+\]/g;
	var pattern2 = /\[[\u4e00-\u9fa5]+\]/;

	if (contents) {
		contents = contents.replace(new RegExp(/\<br>/, 'g'), "\n");
		contents = contents.replace(new RegExp(/\<br\/>/, 'g'), "\n");
	}
	var content = contents.match(pattern1);
	var str = contents;
	var now_str = contents;
	var arr = [];
	if (content == null)
		return [{
			type: 1,
			value: str
		}];
	for (var i = 0; i < content.length; i++) {
		var src = "";
		for (var j = 0; j < emotions.length; j++) {
			if (emotions[j].name == content[i]) {
				src = emotions[j].image;
				break;
			}
		}
		if (!now_str) {
			break;
		}
		var has_i = now_str.indexOf(content[i]);
		if (has_i != -1) {
			arr.push({
				type: 1,
				value: now_str.substring(0, Number(has_i))
			});
			arr.push({
				type: 2,
				value: src
			});
			now_str = now_str.substring(Number(has_i) + Number(content[i].length));
			if (i == (content.length - 1)) {
				arr.push({
					type: 1,
					value: now_str
				});
			}
		}
	}
	return arr;
}

function showToast(msg, cb) {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 2000,
		complete: function() {
			typeof cb == "function" && cb();
		}
	});
}

function formatMsgTime(timestamp) {
	var mistiming = Math.round((Date.now() - timestamp) / 1000);
	var arrr = ['年', '个月', '星期', '天', '小时', '分钟', '秒'];
	var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
	for (var i = 0; i < arrn.length; i++) {
		var inm = Math.floor(mistiming / arrn[i]);
		if (inm != 0) {
			return inm + arrr[i] + '前';
		}
	}
}